<script lang="ts">
export default {
  name: "Pie"
};
</script>

<script setup lang="ts">
import { ECharts } from "echarts";
import echarts from "/@/plugins/echarts";
import { onMounted, nextTick, ref, watch } from "vue";
import { tryOnUnmounted, useTimeoutFn } from "@vueuse/core";
// import { useAppStoreHook } from "/@/store/modules/app";

import {
  addResizeListener,
  removeResizeListener
} from "element-plus/lib/utils/resize-event";

// const pureApp = useAppStoreHook();
const props = defineProps({
  echartData: {
    type: Array,
    default: () => []
  }
});

const pie = ref<HTMLElement | null>(null) as any;

let echartInstance: ECharts;

function initechartInstance(data) {
  const echartDom = document.querySelector(".pie");
  if (!echartDom) return;
  // @ts-ignore
  echartInstance = echarts.init(echartDom);
  echartInstance.clear(); //清除旧画布 重新渲染

  const xArr = data.map(item => item.name);

  echartInstance.setOption({
    // color: ["#00e6ad", "#59a0ff", "#ffc500"],
    color: ["#FAB05D", "#FFE58F", "#BAE637", "#85A5FF", "#FFC069", "#B5F5EC"],

    tooltip: {
      trigger: "item",
      confine: true
    },
    legend: {
      show: false,
      orient: "vertical",
      right: true
    },
    label: {
      show: true,
      position: "top"
    },
    grid: {
      right: 0,
      bottom: 46
    },
    xAxis: [
      {
        type: "category",
        axisTick: {
          alignWithLabel: true
        },
        splitLine: {
          show: false
        },
        axisLabel: {
          interval: 0,
          width: "40",
          overflow: "break"
          // rotate: 20
        },
        data: xArr
      }
    ],
    yAxis: [
      {
        type: "value"
      }
    ],
    series: [
      {
        name: "",
        type: "bar",
        barWidth: 12,
        // label: {
        //   formatter: "{b} {c} ({d}%)",
        //   width: "100",
        //   overflow: "break"
        // },
        // radius: "45%",
        radius: ["30%", "46%"],
        data,
        // [
        //   { value: 1079, name: "watchers" },
        //   { value: 1079, name: "star" },
        //   { value: 204, name: "forks" },
        //   { value: 3, name: "open_issues" }
        // ],
        emphasis: {
          itemStyle: {
            shadowBlur: 10,
            shadowOffsetX: 0,
            shadowColor: "rgba(0, 0, 0, 0.5)"
          }
        }
      }
    ]
  });
}

onMounted(() => {
  nextTick(() => {
    initechartInstance(props.echartData);
    addResizeListener(pie.value, resetRender);
    // useEventListener("resize", () => {
    //   resetRender();
    // });
  });
});

watch(
  () => props.echartData,
  newVal => {
    initechartInstance(newVal);
  }
);

const resetRender = () => {
  if (!echartInstance) return;
  useTimeoutFn(() => {
    echartInstance.resize();
  }, 180);
};

tryOnUnmounted(() => {
  if (!echartInstance) return;
  echartInstance.dispose();
  echartInstance = null;
  removeResizeListener(pie.value, resetRender);
});
</script>

<template>
  <div ref="pie" style="height: 100%">
    <!-- <div class="title">
      <slot> </slot>
    </div> -->
    <div class="pie"></div>
  </div>
</template>

<style lang="scss" scoped>
.title {
  font-size: 16px;
  color: $defaultColor;
  margin: 10px 0;
  height: 24px;
}
.pie {
  width: 100%;
  height: 300px;
}
</style>
